package com.zr.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zr.mapper.GoodsMapper;
import com.zr.pojo.Goods;
import com.zr.service.GoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsMapper mapper;

    //根据商品类型查询列表 销量前4
    @Override
    public List<Goods> selectTypeAll(String type) {
        List<Goods> goods_type = mapper.selectTypeAll(type);
        return goods_type;
    }

    //根据商品类型查询列表
    @Override
    public PageInfo<Goods> readmoreShop(String type,Integer pageNum,Integer pageSize) {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("type",type);
        //利用PageHelper分页查询
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> goods = mapper.selectList(wrapper);
        PageInfo<Goods> pageInfo = new PageInfo<>(goods,10);
        return pageInfo;
    }

    //根据商品id查询商品对象
    @Override
    public Goods selectOnt(Integer gid) {
        QueryWrapper<Goods> wrapper = new QueryWrapper();
        wrapper.eq("gid",gid);
        Goods goods_One = mapper.selectOne(wrapper);
        return goods_One;
    }

    //查询所有商品销量前2的商品
    @Override
    public List<Goods> selectNumAll() {
        List<Goods> goods_Num = mapper.selectNumAll();
        return goods_Num;
    }

    //模糊查询
    @Override
    public PageInfo<Goods> selectLike(String title,Integer pageNum, Integer pageSize) {
        QueryWrapper wrapper = new QueryWrapper();
        Goods goods = new Goods();
        goods.setTitle(title);
        wrapper.like(StringUtils.isNotBlank(goods.getTitle()),"title",goods.getTitle());

        //利用PageHelper分页查询
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> list = mapper.selectList(wrapper);
        PageInfo<Goods> pageInfo = new PageInfo<>(list,10);
        return pageInfo;
    }
}
